/**
 * Copyright (C) 2018-2019
 * All rights reserved, Designed By www.joolun.com
 */
package com.ruoyi.mall.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.mall.api.RemoteGoodsCategoryService;
import com.ruoyi.mall.api.entity.GoodsCategory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;


/**
 * 商品类目
 *
 * @author www.joolun.com
 * @date 2019-08-12 11:46:28
 */
@Slf4j
@RestController
@RequestMapping("/goodscategory")
@Api(value = "goodscategory", tags = "商品类目管理")
public class GoodsCategoryController extends BaseController {
    @DubboReference
    private RemoteGoodsCategoryService goodsCategoryService;

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param goodsCategory 商品类目
     * @return
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    @SaCheckPermission("mall:goodscategory:index")
    public R getGoodsCategoryPage(Page page, GoodsCategory goodsCategory) {
        return R.ok(goodsCategoryService.page(page, goodsCategory));
    }

    /**
     * 返回树形集合
     *
     * @return
     */
    @ApiOperation(value = "返回树形集合")
    @GetMapping("/tree")
    @SaCheckPermission("mall:goodscategory:index")
    public R getGoodsCategoryTree() {
        return R.ok(goodsCategoryService.selectTree(null));
    }

    /**
     * 通过id查询商品类目
     *
     * @param id
     * @return R
     */
    @ApiOperation(value = "通过id查询商品类目")
    @GetMapping("/{id}")
    @SaCheckPermission("mall:goodscategory:get")
    public R getById(@PathVariable("id") String id) {
        return R.ok(goodsCategoryService.getById(id));
    }

    /**
     * 新增商品类目
     *
     * @param goodsCategory 商品类目
     * @return R
     */
    @ApiOperation(value = "新增商品类目")
    @PostMapping
    @SaCheckPermission("mall:goodscategory:add")
    public R save(@RequestBody GoodsCategory goodsCategory) {
        return R.ok(goodsCategoryService.save(goodsCategory));
    }

    /**
     * 修改商品类目
     *
     * @param goodsCategory 商品类目
     * @return R
     */
    @ApiOperation(value = "修改商品类目")
    @PutMapping
    @SaCheckPermission("mall:goodscategory:edit")
    public R updateById(@RequestBody GoodsCategory goodsCategory) {
        if (goodsCategory.getId().equals(goodsCategory.getParentId())) {
            return R.fail("不能将本级设为父类");
        }
        return R.ok(goodsCategoryService.updateById(goodsCategory));
    }

    /**
     * 通过id删除商品类目
     *
     * @param id
     * @return R
     */
    @ApiOperation(value = "通过id删除商品类目")
    @DeleteMapping("/{id}")
    @SaCheckPermission("mall:goodscategory:del")
    public R removeById(@PathVariable String id) {
        return R.ok(goodsCategoryService.removeById(id));
    }

}
